//
//  ByNetBaseModel.m
//  beibei_ios
//
//  Created by hebidu on 16/4/15.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByNetBaseModel.h"

#import <objc/runtime.h>
@implementation ByNetBaseModel

- (NSString *)description {
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    
    unsigned int numIvars;
    Ivar *vars = class_copyIvarList([self class], &numIvars);
    
    NSString *key = nil;
    
    for(int i = 0; i < numIvars; i++) {
        
        Ivar thisIvar = vars[i];
        key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];  //获取成员变量的名字
        NSString *dic_key = [key substringFromIndex:1]; // 去掉首位的"_"
        SEL sel = NSSelectorFromString(dic_key); // 用字符串获得get方法

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        if ([self respondsToSelector:sel]) {
            id dic_value = [self performSelector:sel]; // 调用get方法获得属性值
            
            if (dic_value) { // 在属性值不为空的情况下给字典赋值
                if ([dic_value isKindOfClass:[NSArray class]]) {
                    dic_value = [((NSArray *)dic_value) componentsJoinedByString:@"\n"];
                    dic_value = @"This property is NSArray, Please log it lonely";
                }
                [dic setObject:dic_value forKey:dic_key];
            }
        }
        
        
#pragma clang diagnostic pop
        
    }
    free(vars);
    
    return [NSString stringWithFormat:@"%@", dic];
    
}


- (id)initContentWithDic:(NSDictionary *)jsonDic {
    self = [super init];
    if (self != nil) {
        [self setAttributes:jsonDic];
    }
    
    return self;
}

- (void)setAttributes:(NSDictionary *)jsonDic {
    
    /**
     key:  json字典的key名
     value: model对象的属性名
     */
    // mapDic： 属性名与json字典的key 的映射关系
    NSDictionary *mapDic = [self attributeMapDictionary:jsonDic];
    
    for (NSString *jsonKey in mapDic) {
        
        // modelAttr : "newsId"
        // jsonKey   : "id"
        NSString *modelAttr = [mapDic objectForKey:jsonKey];
        SEL seletor = [self stringToSel:modelAttr];

        
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

        // 判断self 是否有seletor 方法
        if ([self respondsToSelector:seletor]) {
            // json字典中的value
            id value = [jsonDic objectForKey:jsonKey];
            
            if ([value isKindOfClass:[NSNull class]]) {
                value = @"";
            }

                        // 调用属性的设置器方法，参数是json的value
            [self performSelector:seletor withObject:value];

        }
#pragma clang diagnostic pop
     

    }
}

/**
 SEL 类型的创建方式有两种，例如：setNewsId: 的SEL类型
 1.第一种
 SEL selector = @selector(setNewsId:)
 2.第二种
 SEL selector = NSSelectorFromString(@"setNewsId:");
 */

/** 将属性名转成SEL类型的set方法 */
/** newsId --> setNewsId: */
- (SEL)stringToSel:(NSString *)attName {
    // 截取首字母
    // title
    NSString *first = [[attName substringToIndex:1] uppercaseString];// T
    NSString *end = [attName substringFromIndex:1];// itle
    
    NSString *setMethod = [NSString stringWithFormat:@"set%@%@:",first,end];
    // setTitle:
    
    
    // 将字符串转成SEL类型
    return NSSelectorFromString(setMethod);
}

/**
 属性名与json字典中key的映射关系
 key:  json字典的key名
 value: model对象的属性名
 */
- (NSDictionary *)attributeMapDictionary:(NSDictionary *)jsonDic {
    
    NSMutableDictionary *mapDic = [NSMutableDictionary dictionary];
    
    for (id key in jsonDic) {
        [mapDic setObject:key forKey:key];
    }
    
    return mapDic;
}




@end
